﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Enterprise.Infrastructure;
namespace Enterprise.Service.Kaifabu
{
    /// <summary>
    /// 产量Service
    /// </summary>
    public class ChanliangService
    {
        private readonly Enterprise.Data.Kaifabu.ChanliangData dal = new Data.Kaifabu.ChanliangData();

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        /// <summary>
        /// 创建空表
        /// </summary>
        /// <param name="ny"></param>
        /// <param name="dw"></param>
        public void Create(int nf, string dw)
        {
            dal.Create(nf, dw);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="sql"></param>
        public void Update(string sql)
        {
            Enterprise.Data.Common.Data data = new Data.Common.Data();
            data.ExcuteSql(sql);
        }

        /// <summary>
        /// 首页产量数据调用
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public static string ChanLiangForIndex(int year)
        {
            Enterprise.Data.Common.Data dal = new Data.Common.Data();
            string sql = "select dwmc from kfb_chanliangdw ";
            DataSet dwds = dal.GetDataSet(sql);
            string sqlappend = "";
            foreach (DataRow dr in dwds.Tables[0].Rows)
            {
                sqlappend += " decode(sum(case when dwmc='" + dr[0] + "' then CL else 0 end),0,null,sum(case when dwmc='" + dr[0] + "' then CL else 0 end)) as  " + dr[0] + ",";
            }

            sql = "select YEAR," + sqlappend.Remove(sqlappend.Length - 1);
            sql += " from kfb_chanliang where YEAR =" + year + "  group by YEAR,MONTH order by MONTH ASC";
            //System.Web.HttpContext.Current.Response.Write(sql);
            //System.Web.HttpContext.Current.Response.End();
            DataSet ds = dal.GetDataSet(sql);
            StringBuilder sb = new StringBuilder();
            sb.Append("<table id='chanliang'>");
            sb.Append("<tr>");
            foreach (DataColumn dc in ds.Tables[0].Columns)
            {
                sb.Append( "<th>" + dc.ColumnName + "</th>");
            }
            sb.Append("</tr>");
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sb.Append("<tr>");
                foreach (DataColumn dc in ds.Tables[0].Columns)
                {
                    sb.Append("<td>" + dr[dc] + "</td>");
                }
                sb.Append("</tr>");

            }
            sb.Append("</table>");
            return sb.ToString();
        }
    }
}
